package model

import (
	"io"
	"mime/multipart"
	db "mwwow_service_go/database"
	"os"
)

type Article struct {
	Id        int    `json:"id",form:"id"`
	Title     string `json:"title",form:"title"`
	Content   string `json:"content",form:"content"`
	Introduce string `json:"introduce",form:"introduce"`
	AddTime   int    `json:"addTime",form:"addTime"`
	TypeName  string `json:"typeName",form:"typeName"`
	Username  string `json:"username",form:"username"`
}

type ArticleById struct {
	Id        int    `json:"id",form:"id"`
	Title     string `json:"title",form:"title"`
	Content   string `json:"content",form:"content"`
	Introduce string `json:"introduce",form:"introduce"`
	AddTime   int    `json:"addTime",form:"addTime"`
	TypeName  string `json:"typeName",form:"typeName"`
	Username  string `json:"username",form:"username"`
	TypeId    int    `json:"typeId",form:"typeId"`
	UserId    int    `json:"userId",form:"userId"`
}

type ArticleRequest struct {
	Id        int    `json:"id",form:"id"`
	Title     string `json:"title",form:"title"`
	Content   string `json:"content",form:"content"`
	Introduce string `json:"introduce",form:"introduce"`
	AddTime   int    `json:"addTime",form:"addTime"`
	TypeId    int    `json:"typeId",form:"typeId"`
	UserId    int    `json:"userId",form:"userId"`
}

type DeleteArticleRequest struct {
	Id int `json:"id",form:"id"`
}

// 添加文章
func (a *ArticleRequest) SaveArticle() (id int64, err error) {
	var sql = "INSERT INTO article(title,content,introduce,addTime,typeId,userId) VALUES (?,?,?,?,?,?)"
	rs, err := db.SqlDB.Exec(sql, a.Title, a.Content, a.Introduce, a.AddTime, a.TypeId, a.UserId)
	if err != nil {
		return
	}
	id, err = rs.LastInsertId()
	if err != nil {
		return
	}
	return
}

// 删除文章
func (a *ArticleRequest) DeleteArticle(id string) (isSuccess bool, err error) {
	sql := "DELETE FROM article WHERE id=?"
	rs, err := db.SqlDB.Exec(sql, id)
	if err != nil {
		return
	}
	if num, _ := rs.RowsAffected(); num == 0 {
		return
	}
	isSuccess = true
	return
}

// 修改文章
func (a *ArticleRequest) UpdateArticle(id string) (isSuccess bool, err error) {
	sql := "UPDATE article SET title=?,content=?,introduce=?,addTime=?,typeId=?,userId=? WHERE id=?"
	_, err = db.SqlDB.Exec(sql, a.Title, a.Content, a.Introduce, a.AddTime, a.TypeId, a.UserId, id)
	if err != nil {
		return
	}
	isSuccess = true
	return
}

// 获取全部文章
func (a *Article) GetArticle(pageNum int, pageSize int) (articles []Article, total int, err error) {
	articles = make([]Article, 0)
	sql := "SELECT SQL_CALC_FOUND_ROWS article.id as id,article.title as title,article.introduce as introduce," +
		"article.addTime as addTime,type.typeName as typeName,user.username as username " +
		"FROM article LEFT JOIN type ON article.typeId=type.id LEFT JOIN user ON " +
		"article.userId=user.id ORDER BY article.id DESC " +
		"LIMIT ?,? "
	totalSql := "SELECT FOUND_ROWS()"
	rows, err := db.SqlDB.Query(sql, pageSize*(pageNum-1), pageSize)
	if err != nil {
		return
	}
	defer rows.Close()
	for rows.Next() {
		var article Article
		rows.Scan(&article.Id, &article.Title, &article.Introduce, &article.AddTime, &article.TypeName, &article.Username)
		articles = append(articles, article)
	}
	if err = rows.Err(); err != nil {
		return
	}
	row := db.SqlDB.QueryRow(totalSql)
	row.Scan(&total)
	return
}

// 通过类型 id 获取文章列表
func (a *Article) GetArticleByType(id string, pageNum int, pageSize int) (articles []Article, total int, err error) {
	articles = make([]Article, 0)
	sql := "SELECT article.id as id,article.title as title,article.introduce as introduce," +
		"article.addTime as addTime,type.typeName as typeName,user.username as username " +
		"FROM article LEFT JOIN type ON article.typeId=type.id LEFT JOIN user ON " +
		"article.userId=user.id WHERE article.typeId=? ORDER BY article.id DESC " +
		"LIMIT ?,? "
	totalSql := "SELECT FOUND_ROWS()"
	rows, err := db.SqlDB.Query(sql, id, pageSize*(pageNum-1), pageSize)
	if err != nil {
		return
	}
	defer rows.Close()
	for rows.Next() {
		var article Article
		rows.Scan(&article.Id, &article.Title, &article.Introduce, &article.AddTime, &article.TypeName, &article.Username)
		articles = append(articles, article)
	}
	if err = rows.Err(); err != nil {
		return
	}
	row := db.SqlDB.QueryRow(totalSql)
	row.Scan(&total)
	return
}

// 获取网站公告
func (a *Article) GetNotice() (article Article) {
	article = Article{}
	sql := "SELECT article.id as id,article.title as title,article.introduce as introduce," +
		"article.addTime as addTime,type.typeName as typeName,user.username as username " +
		"FROM article LEFT JOIN type ON article.typeId=type.id LEFT JOIN user ON " +
		"article.userId=user.id WHERE article.id=4"
	row := db.SqlDB.QueryRow(sql)
	row.Scan(&article.Id, &article.Title, &article.Introduce, &article.AddTime, &article.TypeName, &article.Username)
	return
}

// 通过文章 id 获取指定文章
func (a *Article) GetArticleById(id string) (article ArticleById, err error) {
	article = ArticleById{}
	sql := "SELECT article.id as id,article.title as title,article.introduce as introduce," +
		"article.content as content,article.typeId as typeId,article.userId as userId," +
		"article.addTime as addTime,type.typeName as typeName,user.username as username " +
		"FROM article LEFT JOIN type ON article.typeId=type.id LEFT JOIN user ON " +
		"article.userId=user.id WHERE article.id=?"
	row := db.SqlDB.QueryRow(sql, id)
	row.Scan(&article.Id, &article.Title, &article.Introduce, &article.Content, &article.TypeId, &article.UserId,
		&article.AddTime, &article.TypeName, &article.Username)
	return
}

// 上传文章图片
func (a *Article) Upload(file multipart.File, header *multipart.FileHeader) (filePath string, err error) {
	filename := header.Filename
	out, err := os.Create("/usr/share/nginx/images/" + filename)
	if err != nil {
		return
	}
	defer out.Close()
	_, err = io.Copy(out, file)
	if err != nil {
		return
	}
	filePath = "http://212.64.78.155/images/" + filename
	return
}
